<?php
namespace UserApi\Controller;
use Zend\Mvc\Controller\AbstractRestfulController;
use Zend\View\Model\JsonModel;
class OrderApiController extends AbstractRestfulController
{
    public function getList()
    {
		header('Access-Control-Allow-Origin: *');
    }
    public function get($id)
    {	
		header('Access-Control-Allow-Origin: *');
		$userMedicineOrderTable=$this->getServiceLocator()->get('Models\Model\UserMedicineOrderFactory');
		$explodeId=explode('-',$id);
		if($explodeId[0]=='user'){
			$user_id=$explodeId[1];
			$totalOrders = $userMedicineOrderTable->totalOrders($user_id);
			if($totalOrders->count()){
				return new JsonModel(array(
					'value'			=>	1,
					'result'		=> json_encode($totalOrders->toArray()),
				));
			}else{
				return new JsonModel(array(
					'value'			=>	0,
				));
			}
		}else if($explodeId[0]=='order'){
			$order_id=$explodeId[1];
			$user_id=$explodeId[2];
			$checkOrder = $userMedicineOrderTable->checkOrder($order_id,$user_id);
			if($checkOrder!=0){
				$orderStatus = $userMedicineOrderTable->orderStatus($order_id)->current()->status;
				if($orderStatus==0){
					return new JsonModel(array(
						'value'		    => 	0,
					));
				}else if($orderStatus==1){
					return new JsonModel(array(
						'value'		    => 1,
					));
				}else if($orderStatus==2){
					return new JsonModel(array(
						'value'		    => 2,
					));
				}else{
					return new JsonModel(array(
						'value'		    => 3,
					));
				}
			}else{
				return new JsonModel(array(
					'value'		    => 4,
				));
			}
		}else{
		   $order_id=$explodeId[1];
		   $order = $userMedicineOrderTable->getuserorder($order_id)->toarray();
		    return new JsonModel(array(
					'value'		    => 1,
					'data' =>json_encode($order),
				));
		}
    }
    public function create($order)
    {	
		header('Access-Control-Allow-Origin: *');
		$prescription_details="";
		$details="";
		$userMedicineOrderTable=$this->getServiceLocator()->get('Models\Model\UserMedicineOrderFactory');
		$userDetailsTable=$this->getServiceLocator()->get('Models\Model\UserDetailsFactory');
		$notificationTable=$this->getServiceLocator()->get('Models\Model\NotificationsFactory');
		if($order['oldorderId']==0){
			if($order['tabletName'][0]!=""){
				foreach($order['tabletName'] as $key=>$tabletName){
					$prescription_details.=$tabletName.'$$'.$order['quantity'][$key].'**';
				}
				$details=trim($prescription_details,'**');
			}
			$images="";
			if($order['Prescriptionn'][0]!=""){
				foreach($order['Prescriptionn'] as $path){
					if($path!=""){
						$content=file_get_contents($path);
						$image=mt_rand();
						$fp = fopen("public/uploads/prescription/".$image.".jpg", "w");
						fwrite($fp, $content);
						fclose($fp);
						$images.=$image.".jpg".'**';
					}
				}
			}
			if($order['hidImages']!=""){
				$images.=$order['hidImages'];
			}
			$imagess=trim($images,'**');
			$sendOrder = $userMedicineOrderTable->saveOrder($order,$details,$imagess);
			
			$sendNotification = $notificationTable->sendAdminNotification($sendOrder,$order,'2','0','1');
			$usersendOrderDetails = $userDetailsTable->updateorderDetailes($order,$order['user_id']);			
		}else{
			$getOrderDetails = $userMedicineOrderTable->getOrderDetails($order['oldorderId']);
			$orderNew=$getOrderDetails->toarray();
			$sendOrder = $userMedicineOrderTable->saveReOrder($orderNew[0]);
			$sendOrderr = $userMedicineOrderTable->changeAutoRenewalReorder($order['oldorderId']);
		}
		if($sendOrder!=0){
			return new JsonModel(array(
				'TrackingId'	=>	$sendOrder,
				'status'		=> 	'success',
				'value'			=>	1,
			));
		}else{
			return new JsonModel(array(
				'status'		=> 'fail',
				'value'			=>	0,
			));
		}
    }
    public function update($order_id,$order)
    {
		header('Access-Control-Allow-Origin: *');
		$userMedicineOrderTable=$this->getServiceLocator()->get('Models\Model\UserMedicineOrderFactory');
		$orderDeliveryTable=$this->getServiceLocator()->get('Models\Model\OrderDeliveryDetailsFactory');
		$notificationTable=$this->getServiceLocator()->get('Models\Model\NotificationsFactory');
		$cancelledOrder = $userMedicineOrderTable->cancelledOrder($order_id);
		$getuserId= $userMedicineOrderTable->getUserId($order_id)->toArray();
		if(count($getuserId)!=0){
			$sendCancelledNotification=$notificationTable->sendcancelledComplaint($order,'1','0','0','0','2',$getuserId[0]['user_id']);
		}
		$cancelledOrderr = $orderDeliveryTable->deleteOrders($order_id);
		if(count($cancelledOrder)!=0){
		   return new JsonModel(array(
					'status'			=>	'success',
					'value'             =>  1,
				));	  
		}else{
		  return new JsonModel(array(
					'status'			=>	'fail',
					'value'             =>  0,
				));		  
		}
    }
    public function delete($id)
    {
       
    }
}